package com.hzya.frame.system.user.service.impl;

import com.hzya.frame.system.user.dao.IUserDao;
import com.hzya.frame.system.user.dao.IUserRoleDao;
import com.hzya.frame.system.user.entity.UserEntity;
import com.hzya.frame.system.user.entity.UserRoleEntity;
import com.hzya.frame.system.user.service.IUserService;
import com.hzya.frame.utils.core.service.BaseService;
import com.hzya.frame.utils.help.AES;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;

import java.io.IOException;
import java.util.List;

@Service(value = "UserService")
public class UserServiceImpl extends BaseService<UserEntity, Long> implements IUserService {

	@Autowired
	protected IUserDao userDao;
	@Autowired
	private IUserRoleDao userRoleDao;

	@Autowired
	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
		this.dao = userDao;
	}

	/***
	 * 保存用户
	 * @param entity
	 * @return
	 */
	@Override
	public UserEntity saveUser(UserEntity entity) throws Exception {
		/* 验证用户是否存在**/
		if(validateLoginName(entity.getLogin_name())){
			throw new Exception("用户名已经存在！");
		}
		MultipartFile icon = entity.getIcon();
		/* 头像不能与空的时候*/
		if (icon !=null && !icon.isEmpty()) {
			entity.setIcon_url(getIcodBase64(icon));
		}
		/* 设置默认密码**/
		entity.setPassword(AES.Encrypt("123456"));
		 userDao.save(entity);
		/* 判断角色是否为空，如果不为空的时候 保存用户角色表*/
		if (entity.getRolesIdList().size() > 0) {
			userRoleSave(entity, entity.getRolesIdList());
		}
		return entity;
	}

	/***
	 * 传入一个file 获取base64字符
	 * @param file
	 * @return
	 * @throws IOException
	 */
	private String getIcodBase64(MultipartFile file) throws IOException {
		String iconBase64 = null;
		BASE64Encoder encoder = new BASE64Encoder();
		byte[] bytes = file.getBytes();
		  iconBase64 = encoder.encode(bytes);
		  return iconBase64;
	}
	/***
	 * 保存用户角色关系
	 * @param userEntity
	 * @param roles
	 */
	private void userRoleSave(UserEntity userEntity, List<Long> roles) {

		/* 删除原来的用户角色*/
		UserRoleEntity removeUserRoleEntity = new UserRoleEntity();
		removeUserRoleEntity.setUser_id(userEntity.getId());
		userRoleDao.logicRemoveMultiCondition(removeUserRoleEntity);

		/* 保存新角色*/
		roles.forEach(value -> {
			UserRoleEntity userRoleEntity = new UserRoleEntity();
			userRoleEntity.setRole_id(value);
			userRoleEntity.setUser_id(userEntity.getId());
			userRoleDao.save(userRoleEntity);
		});
	}

	/**
	 * 验证用户名是否存在！true已经存在，false，不存在
	 * @param loginName
	 * @return
	 */
	public boolean validateLoginName(String loginName){
		boolean flag =false;
		UserEntity userEntity = new UserEntity();
		userEntity.setLogin_name(loginName);
		List list =  userDao.query(userEntity);
		if(list.size() > 0){
			flag =true;
		}
		return  flag;
	}

	/**
	 * 修改用户基本信息
	 *
	 * @param entity
	 */
	@Override
	public void updateUser(UserEntity entity) throws IOException {
		MultipartFile icon = entity.getIcon();
		/* 头像不能与空的时候*/
		if (icon !=null && !icon.isEmpty()) {
			entity.setIcon_url(getIcodBase64(icon));
		}
		userDao.update(entity);
		userRoleSave(entity, entity.getRolesIdList());
	}


}